import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-09-12
 * Time: 18:36
 */
public class total_Fruit {
    public static int totalFruit(int[] fruits) {
        HashMap<Integer, Integer> hash = new HashMap<>();
        int num = 0;
        for (int right = 0, left = 0; right < fruits.length; right++) {
            //进窗口
            int n = hash.getOrDefault(fruits[right], 0);
            hash.put(fruits[right], n + 1);

            //判断
            while (hash.size() > 2) {
                //出窗口
                int m = hash.get(fruits[left]);
                if (m == 1) {
                    hash.remove(fruits[left]);
                }else {
                    hash.put(fruits[left], m - 1);
                }
                left++;
            }
            num = Math.max(num, right-left+1);
        }
        return num;
    }

    public static void main(String[] args) {
        int[] arr=new int[]{3,3,3,1,2,1,1,2,3,3,4};
        System.out.println(totalFruit(arr));
    }
}
